/**
 * @Author: Your name
 * @Date:   2021-10-12 15:07:51
 * @Last Modified by:   Your name
 * @Last Modified time: 2021-10-13 14:57:40
 */
$(function () { 

    function getUrlParams(url) {
        //1.对url进行处理,把?号后面的参数匹配出来,使用正则进行匹配
        //http://127.0.0.1:5500/detail.html?id=20&age=99
        let reg = /^[^\s]+\?([^&\s\d]+=[^\s]+)*/
        let match = reg.exec(url)
        match = match[1]
        if (!match) {//表示没匹配到
            console.warn("没有匹配到相应的query参数")
            return null
        }
        // console.log(match);
        //对参数进行uri解码
        let queryString = decodeURI(match);
        // console.log(queryString);
        //将queryString以"&"符号为分割点 切割成为一个数组
        let queryArr = queryString.split("&");
        // console.log(queryArr);
        let queryParamsObj = {}//用于存储处理好的query数据
        queryArr.forEach(item => {
            let itemArr = item.split("=") //对每个item 'id=20'这样的切割成['id','20']
            queryParamsObj[itemArr[0]] = itemArr[1]
        })
        // console.log(queryParamsObj);
        //将最终处理的结果返回
        return queryParamsObj
    }
    let qr = getUrlParams(location.href);
    // console.log(qr);
        //  console.log(qr.brandtitleid);

        $.ajax({
            url: "http://chst.vip:1234/api/getproduct",
            data:{ 
                
                productid:qr.productid
            }
            
        }).then(res=>{
            console.log(res);
            renders(res.result)
        })
        
        function renders(data=[]) { 
            let html=''
            
            data.forEach(item=>{
                
                $('.backupclearfix').html(`<a href="index.html">首页&gt;</a>
    <a href="category.html">全部分类&gt;</a>
    <a href=>${item.productName}&gt;</a>
    <button>筛选</button>`)
                
                html+=`
                <div class="tops">
                ${item.productImg}
                </div>
                <div class="dtitle">
                ${item.productName}
                </div>
            
                <div class="minprice">当前最低￥6000</div>
            
                <div class="goodspack">
                    <span>优选评论</span>
                    <span>8266条</span>
                </div>
            
                <div class="dbuttom">
                    <span>京东log</span>
                    <a href="">京东商城</a>
                    <span>${item.productPrice}</span>
                    <span>去购买</span>
                </div>
                `    
            })
            $('.divfo').html(html)
         }






        








        
        $.ajax({
            url: "http://chst.vip:1234/api/getproductcom",
            data:{
                productid:qr.productid
            }
            
        }).then(res=>{
            // console.log(res);
            render(res.result)
        })


        function render(data=[]) { 
            let html=''
            
            data.forEach(item=>{
                html+=`
                <div class="cmment">
                    <div class="tp">
                        <span>${item.comName}</span>
                        <span>${item.comTime}</span>
                    </div>
                    <div class="conment">
                    ${item.comContent}
                    </div>
                    <span>${item.comFrom}</span> 
                 </div>
                `    
            })
            $('.pl').html(html);
            
           
         }



         
        

      

         
    

    



    
 })